import collections
class Solution(object):
    def maximumANDSum(self, nums, numSlots):
        now_d = {0:0}
        for num in nums:
            next_d = {}
            for val, score in now_d.items():
                for i in range(numSlots):
                    if (val >> (2 * i)) & 3 < 2:
                        next_val = val + (1 << (2 * i))
                        if next_val not in next_d:
                            next_d[next_val] = score + (num & (i + 1))
                        else:
                            next_d[next_val] = max(next_d[next_val], score + (num & (i + 1)))
            now_d = next_d
        return max(now_d.values())

data = Solution()
nums = [1,2,3,4,5,6]
numSlots = 3
print(data.maximumANDSum(nums, numSlots))
